/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers.classes;

import classes.Breadcrumbs;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Course;
import models.Employee;
import models.Lesson;
import dao.CourseDao;
import classes.HibernateUtil;
import dao.LessonDao;

/**
 * This servlet handles the lists of lessons depending on the parameters given.
 * 
 * @author Vuong Pham<Vuong.Pham@hva.nl>
 */
public class CourseLessonController extends HttpServlet {

    /**
     * This do get method gets a list of lessons by course, free lessons or all lessons.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        boolean isfree = false;

        if (request.getParameter("isFree") != null) {
            isfree = true;
        }

        HibernateUtil hu = new HibernateUtil();
        List<Lesson> lessonList = new ArrayList<Lesson>();
        List<Lesson> freeLessonList = new ArrayList<Lesson>();
        Course course;

        if (request.getParameter("courseid") != null) {
            long courseid = Long.parseLong(request.getParameter("courseid"));

            CourseDao cdao = new CourseDao(hu.getSessionFactory().openSession());
            course = cdao.get(courseid);

            LessonDao ldao = new LessonDao(hu.getSessionFactory().openSession());

            if (isfree) {
                lessonList = ldao.findAllFreeInCourse(courseid);
                request.setAttribute("isFree",true);
            } else {

                lessonList = ldao.findAllByCourse(courseid);
            }

        } else {
            LessonDao ldao = new LessonDao(hu.getSessionFactory().openSession());
            lessonList = ldao.findAll();
            course = new Course();
            course.setName("All");
        }


        request.setAttribute("course", course);
        if (!lessonList.isEmpty()) {
            request.setAttribute("lessonList", lessonList);
        }
        
        // Breadcrumbs
        Breadcrumbs[] breadcrumbs = {
            new Breadcrumbs("Index", "Home"),
            new Breadcrumbs("Category", "Course catalog"),
            new Breadcrumbs("Course?courseid=" + course.getId(), course.getName()),
            new Breadcrumbs("CourseLesson?courseid=" + course.getId(), "Lessons")
        };
        request.setAttribute("breadcrumbs", breadcrumbs);
        
        RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/course_lesson_list.jsp");
        dispatcher.forward(request, response);
    }

}
